﻿using Mva.Recognition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Log = Logger.Debug;
using Debug = Logger.Debug;
using XiaoweiUWP.Settings;
using System.Threading;
using Windows.ApplicationModel.ConversationalAgent;

namespace XiaoweiUWP.Services
{
    public class MVAWakeUpService
    {
        private MVAWakeUpService()
        {

        }
        private static readonly Lazy<MVAWakeUpService> LazyInstance = new Lazy<MVAWakeUpService>(() =>
        {
            return new MVAWakeUpService();
        }, LazyThreadSafetyMode.ExecutionAndPublication);

        public static MVAWakeUpService Instance { get { return LazyInstance.Value; } }
        public event Action WakeupConfirmed;
        public event Action WakeupRejected;
        public bool Inited { get; set; } = false;
        public void Init()
        {
            if (Inited) return;
            Inited = true;
            Recognizer.Initialize("lApXCXO35/liDZKtWe6yag==", "v10z8vjag6ke6");
            Recognizer.RequestAgentStateChangeAsync(ConversationalAgentState.Inactive);
            ChangeBackend();
            Recognizer.SaveKeywordEnabled = DebugConfig.SaveKeywordEnabled;
            Recognizer.KeywordConfirmed += async() =>
            {
                Log.WriteLine($"MVAWakeUpService Recognizer.KeywordConfirmed");
                await Recognizer.StopDetectionAsync().ConfigureAwait(false);
                WakeupConfirmed?.Invoke();
            };

            Recognizer.KeywordRejected += async() =>
            {
                Log.WriteLine($"MVAWakeUpService Recognizer.KeywordRejected");
                await Recognizer.StopDetectionAsync().ConfigureAwait(false);
                WakeupRejected?.Invoke();
            };

            Recognizer.SessionExceptionAccured += (msg) =>
            {
                Debug.WriteLine($"MVAWakeUpService Recognizer.SessionExceptionAccured, msg:{msg}");
            };
            Recognizer.UserAuthenticationChanged += ()=>
            {
                IsLockedChanged?.Invoke();
            };
        }
        public async void RequestAgentStateChangeAsync(ConversationalAgentState state)
        {
            await Recognizer.RequestAgentStateChangeAsync(state).ConfigureAwait(false);
        }

        public async void SetEnableStageKws(bool firstStage, bool secondStage)
        {
            await Recognizer.EnableFirstStageKws(firstStage).ConfigureAwait(false);

            Recognizer.EnableSecondStageKws = secondStage;
            ChangeBackend();
        }
        public void ChangeBackend()
        {
            Recognizer.ChangeBackend(new LocalKeywordRecognizer2());
            Recognizer.SetMsSpeechConfig(DebugConfig.MsSpeechConfig);
        }

        public event Action IsLockedChanged;

        public async void WakeUpForeground()
        {
            Log.WriteLine("Activate Application to show foreground");
            await Recognizer.ForegroundActivationAsync().ConfigureAwait(false);
        }

        public void StartDetection()
        {
            Log.WriteLine("StartDetection");
            Recognizer.StartDetection();
        }


        public async void Suspend()
        {
            await Recognizer.StopDetectionAsync().ConfigureAwait(false);
            Recognizer.BackgroundTaskComplete();
        }

        public async void Resume()
        {
            await Recognizer.ResetAgentSessionAsync().ConfigureAwait(false);
        }

        public async Task<bool> GetIsUserAuthenticated()
        {
            return await Recognizer.GetIsUserAuthenticated().ConfigureAwait(false);
        }



    }

}
